package ui.activity.mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.MallCourseAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.GoodsCourse;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;

/**
 * author : ChenHuiMing
 * date : 2018/9/16
 * email : 907399255@qq.com
 * description : 课程信息
 */
public class MallCourseActivity extends BaseNetActivity {

    public static final String CREATE_ORDER_URL = "/Shop/createOrder";
    public static final String SCHEDULE_DETAIL = "/Shop/scheduleDetail";
    public static final String EXTRA_SCHEDULE_ID = "extra_schedule_id";

    private ImageView mIvBook;
    private TextView mTvTeacher;
    private TextView mTvCourse;
    private TextView mTvTime;
    private TextView mTvHour;
    private TextView mTvPrice;
    private TextView mTvApply;
    private RecyclerView mRvCourse;
    private List<GoodsCourse.CourseRoom> mCourseRooms = new ArrayList<>();

    private String mScheduleId;
    private MallCourseAdapter mCourseAdapter;

    public static void startMallCourseActivity(Context context, String schedule_id) {
        Intent intent = new Intent(context, MallCourseActivity.class);
        intent.putExtra(EXTRA_SCHEDULE_ID, schedule_id);
        context.startActivity(intent);
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (TextUtils.equals(action, SCHEDULE_DETAIL)) {
            GoodsCourse goodsCourse = (GoodsCourse) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsCourse.class);
            mTvTeacher.setText("主讲："+goodsCourse.teacher_name);
            mTvCourse.setText(goodsCourse.schedule_name);
            mTvTime.setText("时间：" + goodsCourse.start_date + " - " + goodsCourse.end_date);
            mTvHour.setText("课时：" + goodsCourse.room_number+"节");
            mTvPrice.setText("￥" + goodsCourse.price);
            ImageLoaderUtils.loadImage(this, goodsCourse.schedule_image, mIvBook);

            mCourseRooms.clear();
            mCourseRooms.addAll(goodsCourse.rooms);
            mCourseAdapter.setNewData(mCourseRooms);
        } else if (TextUtils.equals(action, CREATE_ORDER_URL)) {
            if (baseBean.getCode() == 200) {
                MallApplyActivity.startMallApplyActivity(this, mScheduleId);
                finish();
            } else {
                toast(baseBean.getMsg());
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mall_course;
    }


    @Override
    protected void findViews() {
        mIvBook = getView(R.id.iv_book);
        mTvTeacher = getView(R.id.tv_teacher);
        mTvCourse = getView(R.id.tv_course);
        mTvTime = getView(R.id.tv_time);
        mTvHour = getView(R.id.tv_hour);
        mTvPrice = getView(R.id.tv_price);
        mTvApply = getView(R.id.tv_apply);
        mRvCourse = getView(R.id.rv_course);
    }

    @Override
    protected void formatViews() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRvCourse.setLayoutManager(linearLayoutManager);
        mCourseAdapter = new MallCourseAdapter(R.layout.item_mall_course, mCourseRooms);
        mRvCourse.setAdapter(mCourseAdapter);
        mRvCourse.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.gray)));
        mTvApply.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        Map<String, Object> map = new HashMap<>();
        map.put("schedule_id", mScheduleId);
        post(SCHEDULE_DETAIL, map, BaseBean.class, false);
    }

    @Override
    protected void getBundle(Bundle bundle) {
        setTitle(getResources().getString(R.string.course_information));
        mScheduleId = getIntent().getStringExtra(EXTRA_SCHEDULE_ID);
    }

    @Override
    public void onClick(View view) {
        if (view == mTvApply) {
            Map<String, Object> map = new HashMap<>();
            map.put("schedule_id", mScheduleId);
            post(CREATE_ORDER_URL, map, BaseBean.class, false);
        }
    }
}
